/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#include "ak_sctp.h"

static 	int 	_gTraceLvl_TestSctp = TRACELVL_DEBUG;

#define TEST_COUNT 1000
S_SOCKET 			_gAccSock[TEST_COUNT];
static 	int 		_gAccCnt = 0;
static _t_cbentry 	_gCbAcc;

static 	void 	SctpTest_AccptCB(void * pArg, long lArg, S_SOCKET sockGen)
{
	SYS_TRACE(_gTraceLvl_TestSctp, TRACELVL_NORMAL, "Accept SCTP:[%d]\n", sockGen);
	_gAccSock[_gAccCnt] = 	sockGen;
	_gAccCnt += 1;
}

#if 0
static void 	SctpTest_PeerCb(void * pArg, long vArg)
{
	return;
}
#endif

static void 	SctpTest_Connect(void)
{
	int i, j, port;
	_t_sockSCTP_Cfg 	sctpCfg;
	S_SOCKET 	 		sctpSock[TEST_COUNT];
	_e_sockState 		state;
	port = 12121;
	sctpCfg.maxStm_In = 3;
	sctpCfg.maxStm_Out= 3;
	sctpCfg.max_Attempt	 = 3;
	sctpCfg.RTO_Initial  = 100 ;
	
	for(j=0; j<1; j++){
		for(i=0; i<TEST_COUNT; i++){
			SYS_SLEEP(10);
			sctpSock[i] = SockSctp_Open(INADDR_ANY, port + i + 1, &sctpCfg, _F_NO);
			SockSctp_Connect(sctpSock[i], 0X7F000001, port, &state);
		}
		SYS_SLEEP(1000);
		for(i=0; i<TEST_COUNT; i++){
			SockSctp_Close(sctpSock[i], 0);
		}
	}

	
	SYS_SLEEP(5000);
	return;
}



static 	void 	SctpTest_Accept(void)
{
	int  j, port;
	_t_sockSCTP_Cfg 	sctpCfg;
	S_SOCKET 	 		sctpSock;
	
	port = 12121;
	sctpCfg.maxStm_In = 3;
	sctpCfg.maxStm_Out= 3;
	sctpCfg.max_Attempt	 = 3;
	sctpCfg.RTO_Initial  = 100 ;

	_CBENTRY_INIT(&(_gCbAcc), SctpTest_AccptCB, NULL, 0);
	for(j=0; j<1; j++){
		SYS_SLEEP(10);
		sctpSock = SockSctp_Open(INADDR_ANY, port, &sctpCfg, _F_YES);
	}
	listen(sctpSock, 255);
	do{
		SockSctp_Accept(sctpSock, &_gCbAcc);
		SYS_SLEEP(10);
	}while(1);
	
	SYS_SLEEP(5000);
	return;
}



static 	void SctpTest(void)
{
	_t_sysTask 	sctpCli;
	_t_sysTask 	sctpSvr;

	STASK_CRATE(sctpSvr, 0x10000, 100, SctpTest_Accept, 0);
	SYS_SLEEP(5000);
	STASK_CRATE(sctpCli, 0x10000, 100, SctpTest_Connect, 0);


	do{
		SYS_SLEEP(5000);
	}while(1);
}



int 	main(void)
{
	SctpTest();
	return 0;
}

